class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.column :name, :string
      t.column :description, :string
    end

    create_table :classifications_customers, :id => false do |t|
      t.column :classification_id, :string
      t.column :customer_id, :string
    end
    
    #---------------------------------------------------------------------#
    # Adicinando estrutura de classification para Customer
    #---------------------------------------------------------------------#
    Classification.new(:name => "Customers", :classification_id => Classification.find(1).id).save()
    
    #---------------------------------------------------------------------#
    # Adicinando estrutura de autenticação para Customer
    #---------------------------------------------------------------------#
    # Criando o grupo de Administração
    group = Group.new(:name => "Customer")
    group.save()
    
    # Criando a capabilite de ADM:ALL
    all = Capabilite.new(:acronym => "CSTM", :name => "ALL")
    all.save()
    Capabilite.new(:acronym => "CSTM", :name => "ADD").save()
    Capabilite.new(:acronym => "CSTM", :name => "REMOVE").save()
    Capabilite.new(:acronym => "CSTM", :name => "MODIFY").save()
    Capabilite.new(:acronym => "CLAS", :name => "VIEW").save()

     # Criando a regra de Administração e associando Capacidades
    role = Role.new(:name => "Customer")
    role.capabilites.push(all)
    role.save()
    
    # Associando o grupo de administração com a regra de administração
    group.roles << role
    group.save();

  end

  def self.down
    drop_table :customers
    drop_table :classifications_customers
  end
end
